//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define lll __int128
//#define double long double
#define fi first
#define se second
using namespace std;
const long long INF=1e16;
//const long long mod=998244353,orm=3;
// const long long mod=1000000007;
const int MAXN=1000005;
const double eps=1e-6;
bool ST;
vector<int> lsh,lsh2,a[MAXN],a2[MAXN],ins[MAXN],del[MAXN];
int n,C,NOW=0,CNT=0,N,M;
int b[MAXN],prt[MAXN],to[MAXN],to2[MAXN],L[MAXN],R[MAXN],c1[MAXN],c2[MAXN],ans[MAXN],d[MAXN];
int gf(int x) {
    if(prt[x]==x) return x;
    return prt[x]=gf(prt[x]);
}
void merge(int x,int y) {
    x=gf(x),y=gf(y);
    if(x==y) assert(0);CNT--;
    NOW-=(R[x]-L[x]+1)*C;
    NOW-=(R[y]-L[y]+1)*C;
    prt[x]=y;
    L[y]=L[x];
    NOW+=(R[y]-L[y]+1)*C;
}
void init() {
    sort(lsh.begin(),lsh.end()),lsh.erase(unique(lsh.begin(),lsh.end()),lsh.end());
    lsh2.push_back(1);
    sort(lsh2.begin(),lsh2.end()),lsh2.erase(unique(lsh2.begin(),lsh2.end()),lsh2.end());
    N=lsh.size();M=lsh2.size();
    for(int i=1;i<=N;i++) to[i]=lsh[i-1];
    for(int i=1;i<=n;i++) {
        for(int j=1;j<a[i].size();j++) a[i][j]=lower_bound(lsh.begin(),lsh.end(),a[i][j])-lsh.begin()+1;
    }
    for(int i=1;i<=M;i++) to2[i]=lsh2[i-1];
    for(int i=1;i<=n;i++) {
        for(int j=1;j<a2[i].size();j++) a2[i][j]=lower_bound(lsh2.begin(),lsh2.end(),a2[i][j])-lsh2.begin()+1;
    }
}
void solve() {
    cin>>n;
    for(int i=1;i<=n;i++) {
        int k;cin>>k;a[i].push_back(-1);a2[i].push_back(-1);
        for(int j=1,x;j<=k;j++) cin>>x,a[i].push_back(x),lsh.push_back(x);
        for(int j=2;j<=k;j++) a2[i].push_back(a[i][j]-a[i][j-1]+1),lsh2.push_back(a[i][j]-a[i][j-1]+1);
    }
    init();
    cin>>C;
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<=n;i++) {
        int k=a[i].size()-1;
        vector<pair<int,int> > t;
        t.push_back({1,0});
        for(int j=2;j<=k;j++) {
            t.push_back({a2[i][j-1],j});
        }
        NOW=k*C,CNT=k;
        for(int j=1;j<=k;j++) prt[j]=j,L[j]=R[j]=to[a[i][j]];
        sort(t.begin(),t.end());
        int lst=0;
        for(int j=0;j<t.size();j++) {
            int x=t[j].fi,y=t[j].se;
            if(y) merge(y-1,y);
            if(j==t.size()-1||x!=t[j+1].fi) {
                int NOW2=NOW+CNT*C*(to2[x]-1);
                // int l=x,r=10000;
                // if(j+1<t.size()) r=t[j+1].fi-1;
                // cout<<i<<" "<<j<<" "<<l<<" "<<r<<" "<<CNT*C<<" "<<NOW2-CNT*C*x<<"\n";
                c1[x]+=NOW2-CNT*C*to2[x];
                if(j+1<t.size()) c1[t[j+1].fi]-=NOW2-CNT*C*to2[x];
                c2[x]+=CNT*C;
                if(j+1<t.size()) c2[t[j+1].fi]-=CNT*C;
            }
        }
        for(int j=1;j<=k;j++) {
            int pre=0;
            if(j==1) pre=M+1;
            else pre=a2[i][j-1];
            ins[1].push_back(a[i][j]);
            del[pre].push_back(a[i][j]);
        }
    }
    for(int i=1;i<=M;i++) c1[i]+=c1[i-1],c2[i]+=c2[i-1];
    for(int i=1;i<=M;i++) ans[i]=c1[i]+c2[i]*to2[i];
    // for(int i=1;i<=M;i++) cout<<to2[i]<<" "<<ans[i]<<"\n";
    int res=0;
    for(int i=1;i<=M;i++) {
        for(int j:ins[i]) {
            res-=d[j]*b[d[j]];
            d[j]++;
            res+=d[j]*b[d[j]];
        }
        for(int j:del[i]) {
            res-=d[j]*b[d[j]];
            d[j]--;
            res+=d[j]*b[d[j]];
        }
        ans[i]+=res;
    }
    // for(int i=1;i<=M;i++) cout<<ans[i]<<" ";cout<<"\n";
    int ans2=INF;vector<int> ans3;
    for(int i=1;i<=M;i++) ans2=min(ans2,ans[i]);
    for(int i=1;i<=M;i++) if(ans2==ans[i]) ans3.push_back(to2[i]);
    cout<<ans2<<" "<<ans3.size()<<"\n";
    for(int i:ans3) cout<<i<<" ";
}
bool ED;
signed main() {
	// cout<<(&ST-&ED)/1024.0/1024.0;
	freopen("list.in","r",stdin);
	freopen("list.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    solve();
	return 0;
}